package fm.dongman.animefm.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.w3c.dom.Text;

import java.util.List;
import java.util.Objects;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.modellib.model.MsgModel;

/**
 * Created by Administrator on 2017/12/2.
 */

public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ViewHolder>{

    private List<IModel.IMsgModel> mListMsg;
    private Context mContext;
    private String myId;

    public ChatMessageAdapter(List<IModel.IMsgModel> ListMsg, Context Context,String myId){
        mListMsg=ListMsg;
        mContext=Context;
        this.myId=myId;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_chat_message,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MsgModel msg= (MsgModel) mListMsg.get(position);
        String UserId=msg.getUserId();

        if(!Objects.equals(UserId, myId)){
            holder.mLayoutRight.setVisibility(View.GONE); //隐藏
            holder.mLayoutLeft.setVisibility(View.VISIBLE); //显示

            Glide.with(mContext).load(msg.getUserUrl()).into(holder.imgleft);
            holder.userNameleft.setText(msg.getmSenderName());
            holder.userMesstLeft.setText(msg.getContent());
        }else{
            holder.mLayoutLeft.setVisibility(View.GONE); //隐藏
            holder.mLayoutRight.setVisibility(View.VISIBLE); //显示

            Glide.with(mContext).load(msg.getUserUrl()).into(holder.imgRight);
            holder.userNameRight.setText(msg.getmSenderName());
            holder.userMesstRight.setText(msg.getContent());
        }




    }

    @Override
    public int getItemCount() {
        return mListMsg.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout mLayoutLeft;
        private ImageView imgleft;
        private TextView userNameleft;
        private TextView userMesstLeft;

        private RelativeLayout mLayoutRight;
        private ImageView imgRight;
        private TextView userNameRight;
        private TextView userMesstRight;

        public ViewHolder(View itemView) {
            super(itemView);
            mLayoutLeft= (RelativeLayout) itemView.findViewById(R.id.RlChatMessBoxLeft);
            imgleft= (ImageView) itemView.findViewById(R.id.mIvLeftChatMessInfoImg);
            userNameleft= (TextView) itemView.findViewById(R.id.mTvChatMessLeft);
            userMesstLeft= (TextView) itemView.findViewById(R.id.mTvChatMessBoxLeft);

            mLayoutRight= (RelativeLayout) itemView.findViewById(R.id.RlChatMessBoxRight);
            imgRight= (ImageView) itemView.findViewById(R.id.mIvLeftChatMessInfoImgRight);
            userNameRight= (TextView) itemView.findViewById(R.id.mTvChatMessRight);
            userMesstRight= (TextView) itemView.findViewById(R.id.mTvChatMessBoxRight);
        }
    }
}
